﻿using HalconDotNet;
using LeaderCCSLaserUI.Services;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ViewROI;

namespace LeaderCCSLaserUI.ViewModels.Dialogs
{
    public class MessageAlarmDialogViewModel : DialogViewModel
    {
        #region 变量
        private readonly IPLCService pLC;
        private CancellationTokenSource source0;
        #endregion
        #region 属性
        private string messageStr;
        public string MessageStr
        {
            get { return messageStr; }
            set { SetProperty(ref messageStr, value); }
        }
        #endregion
        #region 方法
        private DelegateCommand<object> operateCommad;
        public DelegateCommand<object> OperateCommad =>
            operateCommad ?? (operateCommad = new DelegateCommand<object>(ExecuteOperateCommad));

        void ExecuteOperateCommad(object obj)
        {
            switch (obj.ToString())
            {
                case "确定":
                    OnRequestClose(new DialogResult(ButtonResult.Yes));
                    break;
                case "取消":
                    OnRequestClose(new DialogResult(ButtonResult.No));
                    break;
                default:
                    break;
            }
        }
        #endregion
        #region 导航
        public override void OnDialogClosed()
        {
            if (source0 != null)
            {
                source0.Cancel();
            }
        }
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            try
            {
                MessageStr = parameters.GetValue<string>("Message");
               
                source0 = new CancellationTokenSource();
                CancellationToken token = source0.Token;
                Task.Run(() => Run(token), token);
            }
            catch { }
        }
        #endregion
        #region 构造函数
        public MessageAlarmDialogViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            MessageStr = string.Empty;
            Title = "报警";
            pLC = containerProvider.Resolve<IPLCService>("PLC");
        }
        #endregion
        #region 功能函数
        private void Run(CancellationToken token)
        {
            while (true)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }
                try
                {
                    var eMGButton = pLC.XCoils_X0[3] && pLC.XCoils_X0[7];
                    var startButton = pLC.XCoils_X0[0] || pLC.XCoils_X0[4];
                    var resetButton = pLC.XCoils_X0[1] || pLC.XCoils_X0[5];
                    if (!eMGButton || resetButton)
                    {
                        System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            OnRequestClose(new DialogResult(ButtonResult.No));
                        }));
                        break;
                    }
                    else if (startButton)
                    {
                        System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                        {                          
                            OnRequestClose(new DialogResult(ButtonResult.Yes));
                        }));
                        break;
                    }
                }
                catch { }
                System.Threading.Thread.Sleep(200);
            }
        }
        #endregion
    }
}
